package com.juzipi.demo.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.juzipi.demo.mapper.StudentMapper;
import com.juzipi.demo.pojo.Student;
import com.juzipi.demo.tools.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class StudentService {


    @Autowired
    private StudentMapper studentMapper;


//    public List<Student> queryStudentAll() {
//        return studentMapper.selectList(null);
//    }


    public PageResult<Student> queryStudentByName(String key, Long pageNum, Long pageSize) {

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        //模糊查询多个字段吧，这里不知道怎么写用了一个笨一点的方法
        queryWrapper.like("name",key).or().like("age",key).or().like("gender",key).or().like("email",key).or().like("birthday",key).or().like("info",key);

        Page<Student> pageInfo = new Page<>(pageNum,pageSize);
        Page<Student> page = studentMapper.selectPage(pageInfo, queryWrapper);
        PageResult<Student> pageResult = new PageResult<>(page.getTotal(),page.getPages(),page.getRecords());

        return pageResult;

    }


    @Transactional
    public void modifyStudent(Student student) {

        int i = studentMapper.updateById(student);
        if (i == 0){
            System.out.println("修改失败");
        }

    }



    @Transactional
    public void removeStudentById(Long id) {
        //这里为了折线图的计算删除的学生数量
        //在删除之前查询到这个学生的信息
        //然后给它的 deleteTime 字段加一个时间值
        //再把这条数据删除
        Student student = studentMapper.selectById(id);
        student.setDeleteTime(new Date());
        studentMapper.updateById(student);
        //使用mybatis plus 封装好的查询方法它会自动给加一个 判断逻辑删除字段 值的条件 就是这个 WHERE deleted=0
        //这样就是实现了逻辑删除
        int i = studentMapper.deleteById(id);
        if (i == 0){
            System.out.println("删除失败");
        }

    }

    public PageResult<Student> queryStudentByPage(Long pageSize, Long pageNum) {

        Page<Student> pageInfo = new Page<>(pageNum,pageSize);
        //查村条件置为空
        //目的是查询所有学生返回一个分页结果集
        Page<Student> page = studentMapper.selectPage(pageInfo, null);

        //注意参数顺序,分别是 总条数，总页数，每页的数据集合
        return new PageResult<>(page.getTotal(),page.getPages(),page.getRecords());


    }


    @Transactional
    public void addStudent(Student student) {

        int insert = studentMapper.insert(student);
        if (insert == 0){
            System.out.println("新增失败");
        }

    }


    public Student queryStudentById(Long id) {
        Student student = studentMapper.selectById(id);
        return student;
    }


    //恢复学生的 deleted 值为0
    public Integer resetStudentAll() {
        Integer integer = studentMapper.resetStudent();
        if (integer == 0){
            System.out.println("ERROR");
        }
        System.out.println("SUCCESS");
        return integer;
    }




    public List<Map<Object,Object>> queryAllStudentCount() {
        //这个操作有点捞了，目的是查询这几个值得数量
        //但是没有一个好办法能一次查完所有，只能一个一个查了
        //方法有点笨了
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        Integer studentCount = studentMapper.selectCount(queryWrapper);

        QueryWrapper<Student> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("gender","男");
        Integer boyCount = studentMapper.selectCount(queryWrapper1);

        QueryWrapper<Student> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("gender","女");
        Integer girlCount = studentMapper.selectCount(queryWrapper2);

        QueryWrapper<Student> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.lt("age",30);
        Integer youngCount = studentMapper.selectCount(queryWrapper3);

        QueryWrapper<Student> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.ge("age",30);
        Integer matureCount = studentMapper.selectCount(queryWrapper4);

        HashMap<Object, Object> hashMap1 = new HashMap<>();
        hashMap1.put("key","学生总数");
        hashMap1.put("value",studentCount);
        HashMap<Object, Object> hashMap2 = new HashMap<>();
        hashMap2.put("key","男生");
        hashMap2.put("value",boyCount);
        HashMap<Object, Object> hashMap3 = new HashMap<>();
        hashMap3.put("key","女生");
        hashMap3.put("value",girlCount);
        HashMap<Object, Object> hashMap4 = new HashMap<>();
        hashMap4.put("key","小于30岁");
        hashMap4.put("value",youngCount);
        HashMap<Object, Object> hashMap5 = new HashMap<>();
        hashMap5.put("key","大于30岁");
        hashMap5.put("value",matureCount);
        List<Map<Object,Object>> mapList = new ArrayList<>();
        mapList.add(hashMap1);
        mapList.add(hashMap2);
        mapList.add(hashMap3);
        mapList.add(hashMap4);
        mapList.add(hashMap5);
        return mapList;
    }

    public List<Map<Object, Object>> queryStudentNewData() {

        List<Map<Object, Object>> mapList = studentMapper.queryStudentNewData();
        return mapList;
    }



    public List<Map<Object, Object>> queryStudentDeleteCount() {
        List<Map<Object, Object>> mapList = studentMapper.queryStudentDeleteCount();
        return mapList;

    }
}
